package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Simulator.CarThread;
import Simulator.Simulator;

@SuppressWarnings("serial")
public class SimulatorGUI extends JFrame implements ActionListener{
	
	private JTextField textNumCars, textMaxSpeed, textLengthRoad, textNumIterations, textProbability, textIncomingProbability, textOutputFile;
	private JButton simulateButton, selectFileButton;
	private JTextArea log;
	
	public SimulatorGUI(){
		JPanel textPanel = new JPanel();
		textPanel.setLayout(new GridLayout(7,2));
		
		textNumCars = new JTextField(8);
		textNumCars.setText("20");
		textPanel.add(new JLabel("Number of Cars: "));
		textPanel.add(textNumCars);
		
		textMaxSpeed = new JTextField(8);
		textMaxSpeed.setText("10");
		textPanel.add(new JLabel("Maximum Speed: "));
		textPanel.add(textMaxSpeed);
		
		textLengthRoad = new JTextField(8);
		textLengthRoad.setText("200");
		textPanel.add(new JLabel("Length of the road: "));
		textPanel.add(textLengthRoad);
		
		textNumIterations = new JTextField(8);
		textNumIterations.setText("100");
		textPanel.add(new JLabel("Number of iterations: "));
		textPanel.add(textNumIterations);
		
		textProbability = new JTextField(8);
		textProbability.setText("0.2");
		textPanel.add(new JLabel("Probability of reduce speed: "));
		textPanel.add(textProbability);
		
		textIncomingProbability = new JTextField(8);
		textIncomingProbability.setText("0.6");
		textPanel.add(new JLabel("Incoming Car Probability: "));
		textPanel.add(textIncomingProbability);
		
		textOutputFile = new JTextField(8);
		textOutputFile.setText("out.dat");
		JPanel outputFilePanel = new JPanel();
		outputFilePanel.setLayout(new GridLayout(0,2));
		outputFilePanel.add(textOutputFile);
		selectFileButton = new JButton("Select");
		selectFileButton.addActionListener(this);
		outputFilePanel.add(selectFileButton);
		textPanel.add(new JLabel("Output Filename: "));
		textPanel.add(outputFilePanel);
		
		simulateButton = new JButton("Simulate");
		simulateButton.addActionListener(this);
		
		log = new JTextArea();
		log.setRows(5);
		log.setEditable(false);
		
		this.add(textPanel,BorderLayout.NORTH);
		this.add(simulateButton,BorderLayout.SOUTH);
		this.add(new JScrollPane(log),BorderLayout.CENTER);
		
		this.pack();
		this.setVisible(true);
		this.setLocation(100,100);
		this.setTitle("ITB Traffic Simulator - Prototype 1");
		
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SimulatorGUI gui = new SimulatorGUI();
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==simulateButton){
			log.append("Starting Simulation");
			
			int numCars = Integer.parseInt(textNumCars.getText());
			int maxSpeed = Integer.parseInt(textMaxSpeed.getText());
			int iterations = Integer.parseInt(textNumIterations.getText());
			int lengthRoad = Integer.parseInt(textLengthRoad.getText());
			float probability = Float.parseFloat(textProbability.getText());
			float incomingCarProbability = Float.parseFloat(textIncomingProbability.getText());
			String filename = textOutputFile.getText();
			
			Simulator simulator = new Simulator(numCars,maxSpeed,lengthRoad,probability, incomingCarProbability);
			//JFrame frame = new CarThread();
		  //   frame.show();
			
			// Set output file
			PrintWriter outFile = simulator.printToFile(filename);
			Date date = new Date();
			outFile.println("#Simulation of traffic "
					+ date.toString());
			outFile.println("#number of cars:"+numCars+" maxSpeed:"+maxSpeed
					+" length of the road:"+lengthRoad+" slowDownProbability:"+probability+
					" incomingCarProbability:"+incomingCarProbability+" number of iterations:"+iterations);
			
			// simulate
			simulator.simulate(iterations);
			
			// close output file
			outFile.close();
			
			simulator.generateGnuplotScript(filename+ ".script", filename);
			
			log.append("\nDONE, check output file "+filename+",\nand load script file in gnuplot\n");
		}else if(e.getSource()==selectFileButton){
			JFileChooser fc = new JFileChooser();			
			int returnVal = fc.showSaveDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                textOutputFile.setText(fc.getSelectedFile().getAbsolutePath());;
            }
		}
	}

}
